package cn.tx.three;

import cn.tx.RabbitMQUtils;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

/**
 * fanout 类型交换机的消费者类
 * @author Mxb
 * @version 1.0
 * @date 2020/10/4 17:14
 */
public class Consumer1 {
    @Test
    public void receiveMessage() throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        //得到通道
        Channel channel = connection.createChannel();
        //绑定交换机
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //从通道里面得到一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //把临时队列和交换机绑定
        channel.queueBind(queue,"logs","");
        //接收消息
        channel.basicConsume(queue,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                      AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者【1】接收到消息:"+new String(body));
            }
        });
        System.out.println("消费者【1】启动成功");
        System.in.read();
    }
}
